package com.imy.lua.lib.app;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.provider.Settings;

import com.immomo.mls.annotation.LuaBridge;
import com.immomo.mls.annotation.LuaClass;
import com.immomo.mls.fun.ud.UDArray;
import com.immomo.mls.utils.LVCallback;
import com.immomo.mls.wrapper.callback.IBoolCallback;
import com.imy.common.util.ALog;
import com.imy.common.util.AppUtils;
import com.imy.common.util.ToastUtil;
import com.imy.lua.LuaCore;

import com.imy.lua.helper.PointScaleHelper;
import com.imy.lua.manger.app.AppManger;
import com.imy.lua.manger.callback.CallBackManger;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;

import java.util.List;

@LuaClass(gcByLua = false)
public class UDApp {
  public static final String LUA_CLASS_NAME = "App";
  protected Globals globals;

  public UDApp(Globals globals, LuaValue[] init) {
    this.globals = globals;
    ALog.i("创建UDApp 对象 线程:" + Thread.currentThread());
  }

  public void __onLuaGc() {
    globals = null;
  }

  @LuaBridge
  public void keyEvent(LVCallback lvCallback) {
    ALog.i("设置 keyEvent");
    CallBackManger.get().addCache(CallBackManger.KEY_EVENT,lvCallback);
  }

  @LuaBridge
  public void watch(LVCallback lvCallback) {
    CallBackManger.get().addCache(CallBackManger.WATCH, lvCallback);
  }

  /**
   * 退出app 杀掉自身进程
   */
  @LuaBridge
  public void exit() {
    AppManger.exit();
  }

  /**
   * 启动app
   *
   * @param name
   */
  @LuaBridge
  public void launchApp(String name) {
    AppUtils.openApp(LuaCore.get().getContext(), name);
  }

  @LuaBridge
  public void matchWindow(IBoolCallback lvCallback) {
    CallBackManger.get().addCache(CallBackManger.WINDOWS, lvCallback);
  }

  @LuaBridge
  public void sleep(int time) {
    try {
      Thread.sleep(time);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  @LuaBridge
  public String verCode() {
    return AppUtils.getVersionCode(LuaCore.get().getContext());
  }

  @LuaBridge
  public String verName() {
    return AppUtils.getVersionName(LuaCore.get().getContext());
  }

  /**
   * 吐司弹窗
   *
   * @param
   */
  @LuaBridge
  public void toast(String msg) {
    ToastUtil.toast(LuaCore.get().getContext(), msg);
  }

  /**
   * 通过App名称获取包名
   *
   * @param appname
   * @return
   */
  @LuaBridge
  public String getPackageName(String appname) {

    return AppUtils.getPackageNameByName(LuaCore.get().getContext(), appname);
  }

  /**
   * 打开app的详情页
   *
   * @param name
   */
  @LuaBridge
  public int openSetting(String name) {
    try {
      Intent intent = new Intent();
      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.setData(Uri.fromParts("package", name, null));
      LuaCore.get().getContext().startActivity(intent);
      return 0;
    } catch (Exception e) {
      return -1;
    }
  }

  @LuaBridge
  public int openUrl(String url) {
    try {
      Uri uri = Uri.parse(url);
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      LuaCore.get().getContext().startActivity(intent);
      return 0;
    } catch (Exception e) {
      return -1;
    }

  }

  @LuaBridge
  public UDArray getInstallApp() {
    List<PackageInfo> systemInstallPackage = AppUtils.getSystemInstallPackage(LuaCore.get().getContext());

    return new UDArray(globals, systemInstallPackage);
  }

  @LuaBridge
  public boolean isInstallApp(String name) {
    PackageInfo systemInstallPackage = AppUtils.isSystemInstallPackage(LuaCore.get().getContext(), name);
    return systemInstallPackage != null;
  }

}
